﻿using System;
namespace Calculator.Xroad.Eu.Messages {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="HistoryResponse", Namespace="http://calculator.x-road.eu", Target=typeof(HistoryResponse))]
    public class HistoryResponseSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected HistoryResponse clazz;
        
        public HistoryResponseSerializer(HistoryResponse clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("request");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDate(((System.DateTime)(clazz.Request))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("History");
            if (clazz.History!=null) {
                for (int x = 0; ((clazz.History != null) 
                            && (x < clazz.History.Count)); x = (x + 1)) {
                    if (clazz.History[x]!=null) {
                        Writer.WriteStartElement("item");
                        Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.History[x]))).ToString());
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : History");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="request") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>GetHistoryResponse>request");
                }
                clazz.Request = new global::Xtee.Core.Schema.XsdDate(Reader.ReadElementString("request")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>GetHistoryResponse>request", Reader.LocalName);
            }
            if (Reader.LocalName=="History") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>GetHistoryResponse>History");
                }
                clazz.History = new System.Collections.Generic.List<System.String>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("History");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if ((Reader.IsElementNil() == false)) {
                            clazz.History.Add(new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("item")).Value);
                        }
                        else {
                            clazz.History.Add(null);
                            Reader.Skip();
                        }
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>GetHistoryResponse>History", Reader.LocalName);
            }
        }
    }
}
